package com.huangpuweb.api.controller;


import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Charsets;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 第三方人员点位，信息
 */
@Controller
@RequestMapping(value = "/people")
public class PeopleController {

    Logger logger = LoggerFactory.getLogger(PeopleController.class);

    @RequestMapping("/point")
    public void peoplePoint(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        // 目标地址
        //生产
        String url = "http://173.82.57.28:1080/api/sjxt/peopleCurrentLocation";
        //调试
        //String url = "http://172.82.4.75:1080/api/sjxt/peopleCurrentLocation";
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("content-type","application/json");
        httpGet.setHeader("X-HW-ID","ksh.hw.zhhp.gov");
        httpGet.setHeader("X-HW-APPKEY","UKD09IO1Ww2P0SfM8IBPKg==");
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String result = "";
        if(response.getStatusLine().getStatusCode() == 200){
           // String data = EntityUtils.toString(response.getEntity(), Charsets.UTF_8);
            result = inputStreamToString(entity.getContent());
        }
        httpGet.abort();
        httpClient.close();
        responseData(result,resp);

    }


    @RequestMapping("/point/info")
    public void peoplePointInfo(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String memberid = req.getParameter("memberid");
        // 目标地址
        //生产
        String url = "http://173.82.57.28:1080/api/wg/user/getUserByMemberId"+ "?memberid="+ memberid;
        //调试
       // String url = "http://172.82.4.75:1080/api/wg/user/getUserByMemberId" + "?memberid="+ memberid;
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("content-type","application/json");
        httpGet.setHeader("X-HW-ID","ksh.hw.zhhp.gov");
        httpGet.setHeader("X-HW-APPKEY","UKD09IO1Ww2P0SfM8IBPKg==");
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String result = "";
        if(response.getStatusLine().getStatusCode() == 200){
            // String data = EntityUtils.toString(response.getEntity(), Charsets.UTF_8);
            result = inputStreamToString(entity.getContent());
        }
        httpGet.abort();
        httpClient.close();
        responseData(result,resp);

    }

    private String inputStreamToString(InputStream is) {

        String line = "";
        StringBuilder total = new StringBuilder();

        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is,Charsets.UTF_8));

        try {
            // Read response until the end
            while ((line = rd.readLine()) != null) {
                total.append(line);
            }
        } catch (IOException e) {
//            Log.e(TAG, e.getLocalizedMessage(), e);
        }

        // Return full string
        return total.toString();
    }



    /**
     * 从req中读取数据
     *
     * @param req
     * @return
     */
    private String getRequestBodyString(HttpServletRequest req) {
        StringBuilder builder = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream(), "utf-8"));
            String line;
            while ((line = br.readLine()) != null) {
                builder.append(line);
            }
        } catch (IOException e) {
            // e.printStackTrace();
            return null;
        }
        return builder.toString();
    }

    private void responseErrorData(String string, HttpServletResponse resp) throws Exception {
        // TODO Auto-generated method stub
        JSONObject resultObj = new JSONObject();
        resultObj.put("errorcode", 1);
        resultObj.put("message", string);
        responseData(resultObj.toString(), resp);
    }

    private void responseData(String string, HttpServletResponse resp) throws Exception {
        // TODO Auto-generated method stub
        responseDate("application/json;charset=UTF-8", string, resp);

    }

    private void responseDate(String contentType, String value, HttpServletResponse resp) throws Exception {
        resp.setContentType(contentType);
        resp.getWriter().write(value);
    }

}
